<template>
 <el-dialog 
   width="80%" 
  :close-on-click-modal="false"
  :title="title"
  :model-value="isShow"
  @open="onOpen"
  @close="onClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="70px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="分类名" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item style="margin-bottom: 0" label="标签分类" prop="parentId">
            <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in categoryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
            </el-select>
        </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item style="margin-bottom: 0" label="上级分类" prop="parentId">
          <ZhongyiComponentsTagTree
            v-model="form.parentId"
            :type="form.type"
          />
        </el-form-item>
        </el-col>

        
      </el-row>

      <el-form-item label="介绍" prop="jieShao">
        <el-input v-model="form.jieShao" type="textarea" :rows="20" />
      </el-form-item>
    </el-form>
      
    <template #footer>
      <el-button @click="onClose">取消</el-button>
      <el-button type="primary" @click="onSave">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDetail, save, getAll } from "@/api/zhongyi/yaocaitag";


export default {
  // components: { tag },
  props: {
    data: {
      required: true,
      type: String,
      default: "",
    },
    isShow: {
      require: true,
      type: Boolean,
      default: false,
    },
    title: {
      require: true,
      type: String,
      default: "",
    },
  },
  data() {
    return {
      form: {},
      rules: {},
      options: [],
      normalizer(node) {
        return {
          label: node.name,
          children: node.child,
        };
      },
      categoryOptions: [{
          value: '1',
          label: '药材'
        }, {
          value: '2',
          label: '方剂'
        }
      ]
    }
  },
  mounted() {
    
  },
  methods: {
    async onOpen() {
      let tags = await getAll();

      if (this.data) {
        const user = await getDetail(this.data);
        this.form = user.result;
        // this.form.roles = user.data.roles
      } else {
        this.form = {
          name: "",
          parentId: "",
          jieShao: "",
          type: ''
        };
      }
    },
    async onSave() {
      // if(this.form.parentId == null) {
      //   this.form.parentId = ""
      // }
      //if(this.data) {
      await save(this.form);
      //}
      //else {
      // await addUser(this.form)
      //}
      this.$emit("onChange", this.form);
      this.$emit("update:isShow", false)
     

      //this.$store.commit('zhongyi/SET_TAGS', null)
    },
    onClose() {
      this.$emit("update:isShow", false)
    },
    onSelect(node) {
      // this.form.level = node.level + 1;
    }
  },
  watch: {
    isShow: function() {
      console.log("isShow", this.isShow)
    }
  },
  computed: {
    tagoptions() {
      return this.options
      // const opion = this.options.filter(t=>t.parentId == this.form.parentId);
      // return [{
      //   id: "0",
      //   name: "默认",
      //   level: 0,
      //   child: opion
      // }]
    }
  }
};
</script>


